	
	
	
	**********************
	*
	* GENERATE VARIABLES
	*
	**********************
	
		
	egen estexp_cat = cut(experience), at(0,1,2,3,5,10,20,100)
	
	
	* Standardize TLF and IVA by YEAR
	egen year_group = group(year)
	sort year_group
	
	
	**** STANDARD APPROACH  ****
		
	** TLF
	by year_group: egen tlf_mean = mean(tlf)
	by year_group: egen tlf_sd = sd(tlf)
	by year_group: gen tlf_std = (tlf-tlf_mean)/tlf_sd
	
	** Individual Scores
	foreach var of varlist pr_1p pr_2p pr_3p pr_1m pr_2m{
		by year_group: egen `var'_ymean = mean(`var')
		by year_group: egen `var'_ystdev = sd(`var')
		by year_group: gen `var'_std = (`var'-`var'_ymean)/`var'_ystdev
		drop `var'_ymean `var'_ystdev
	}
	

	local this_varlist "_1p _2p _3p _1m _2m"
	foreach this_var of local this_varlist{
		forvalues i=1/9{
			local var "pr`i'`this_var'"
			by year_group: egen `var'_ymean = mean(`var')
			by year_group: egen `var'_ystdev = sd(`var')
			by year_group: gen `var'_std = (`var'-`var'_ymean)/`var'_ystdev
			drop `var'_ymean `var'_ystdev
		}
	}
	

	** Other variables needed for tables
	sort id year
		
	gen exp_capped = experience
	gen exp_15 = experience >= 1 & experience <=5
	gen exp_13 = experience >= 1 & experience <=3
	replace exp_capped = 15 if experience > 15
	gen exp_capped_squared = exp_capped^2
	gen exp0 = experience == 0
			
	gen posnt3 = nothreat_cycle3 > 0
	
	gen pcm2_posnt3 = postconf_m2 * posnt3
	gen pcp2_posnt3 = postconf_p2 * posnt3
	
	gen pcp1_diff = confdate_1p - obsdate_1p
	replace pcp1_diff = . if pcp1_diff < 0
	gen pcp2_diff = confdate_2p - obsdate_2p
	replace pcp2_diff = . if pcp2_diff < 0
	gen pcm1_diff = confdate_1m - obsdate_1m
	replace pcm1_diff = . if pcm1_diff < 0
	gen pcm2_diff = confdate_2m - obsdate_2m
	replace pcm2_diff = . if pcm2_diff < 0
	
	
** Student-level characteristics
/* VARIABLES NOT INCLUDED
	sort id year
	bysort id (year): gen mean_stu_math_prev = mean_stu_math[_n-1]
	bysort id (year): gen mean_stu_read_prev = mean_stu_read[_n-1]
	bysort id (year): gen mean_frpl_prev = mean_frpl[_n-1]
*/

	
** create hazard variables
	* if year == 2010 then there is no haz_m1
	gen haz_p1_true = 0
	gen haz_m1_true = haz_m1
	replace haz_m1_true = 0 if year == 2010
	
	gen haz_unif_p1_true = 0
	gen haz_unif_m1_true = haz_unif_m1
	replace haz_unif_m1_true = 0 if year == 2010
	
	gen haz_cycle1 = haz_m1_true + haz_p1_true
	gen haz_cycle2 = haz_m1_true + haz_p2
	gen haz_cycle3 = haz_m2 + haz_p2 + haz_p3
	
	gen haz_cycle12 = haz_p1_true + haz_m1_true + haz_p2
	gen haz_cycle123 = haz_p1_true + haz_m1_true + haz_m2 + haz_p2
	gen haz_cycle23 = haz_m1_true + haz_m2 + haz_p2
	
	
	gen log_hc23 = log(1+haz_cycle23)
	gen log_hc3 = log(1+haz_cycle3)
	
	gen haz_u_cycle1 = haz_unif_m1_true + haz_unif_p1_true
	gen haz_u_cycle2 = haz_unif_p2 + haz_unif_m1_true
	gen haz_u_cycle3 = haz_unif_m2 + haz_unif_p2 + haz_unif_p3
	
	gen haz_u_cycle12 = haz_unif_p1_true + haz_unif_m1_true + haz_unif_p2
	gen haz_u_cycle123 = haz_unif_p1_true + haz_unif_m1_true + haz_unif_m2 + haz_unif_p2
	gen haz_u_cycle23 = haz_unif_m1_true + haz_unif_m2 + haz_unif_p2
	
	
	
	gen log_u_hc23 = log(1+haz_u_cycle23)
	gen log_u_hc3 = log(1+haz_u_cycle3)
	
	
	*** POSTCONF ***
	* raw values have negative stuff -- this might be useful
	gen postconf_p1_raw = postconf_p1
	gen postconf_p2_raw = postconf_p2
	gen postconf_p3_raw = postconf_p3
	gen postconf_m1_raw = postconf_m1
	gen postconf_m2_raw = postconf_m2
	
	* replace -postconf with zero -> this means their eval was late
	replace postconf_p1 = 0 if postconf_p1<0
	replace postconf_p2 = 0 if postconf_p2<0
	replace postconf_p3 = 0 if postconf_p3<0
	replace postconf_m1 = 0 if postconf_m1<0
	replace postconf_m2 = 0 if postconf_m2<0
	

	* Add last year's bonus info
	sort id year
	bysort id (year): gen he_lastyear = finrating[_n-1] == 1
	bysort id (year): replace he_lastyear = . if finrating[_n-1] == .
	bysort id (year): gen tlf_he_lastyear = tlf[_n-1] >= 3.5
	bysort id (year): replace tlf_he_lastyear = . if tlf[_n-1] == .
	bysort id (year): gen tlf_me_lastyear = tlf[_n-1] < 2.5
	bysort id (year): replace tlf_me_lastyear = . if tlf[_n-1]==.
	bysort id (year): gen me_lastyear = finrating[_n-1] == 4
	bysort id (year): replace me_lastyear = . if finrating[_n-1] == .
	
	gen tlf_e_lastyear = !tlf_he_lastyear & ! tlf_me_lastyear
	bysort id (year): replace tlf_e_lastyear = . if tlf[_n-1] == .
	
	replace he_lastyear = . if year == 2010
	replace me_lastyear = .  if year == 2010
	replace tlf_he_lastyear = . if year == 2010
	replace tlf_me_lastyear = . if year == 2010
	
		
	bysort id (year): gen finrate_lastyear = finrating[_n-1]
	replace finrate_lastyear = 0 if finrate_lastyear == .
	
	bysort id (year): gen high_stakes_ever = finrating[_n-1] == 1 | finrating[_n-2] == 1 | finrating[_n-1] == 4 | finrating[_n-2] == 4
	
	gen dismiss_threat = me_lastyear
	
	bysort id (year): gen he_twoyearsback = finrating[_n-2] == 4
	replace he_twoyearsback = 0 if he_twoyearsback == .
	gen he_bonusposs = he_lastyear & !he_twoyearsback
	drop he_twoyearsback
	
	gen high_stakes = he_bonusposs | dismiss_threat
	
	* VARIABLES FOR TESTING CORR WITH TLF
	gen ntp2_exp0 = nothreat_p2 * exp0
	
	* Timing correlation check variables
	* need to remove school average timing for ME and P
	
	gen sch_year = string(sch1)+string(year)
	egen sch_year_group = group(sch_year)
	
	local variables "nothreat_cycle1 nothreat_cycle2 nothreat_cycle3 nothreat_m1 nothreat_m2 nothreat_p1 nothreat_p2 nothreat_p3  tlf_std pr_1p pr_2p pr_1m exp0 dismiss_threat he_lastyear tlf_me_lastyear tlf_he_lastyear"
	local shortname "nt1 nt2 nt3 ntm1 ntm2 ntp1 ntp2 ntp3 ivar ivam tlf pr_1p pr_2p pr_1m exp0 dismiss helast tlfmelast tlfhelast"
	local count : word count `variables'
	
	forvalues i=1/`count' {
		local this_var : word `i' of `variables'
		local this_name : word `i' of `shortname'
		egen gm = mean(`this_var'), by(sch_year_group)
		gen  gcm_`this_name' = `this_var' - gm
		drop gm
		di `i'
		di "`this_var'"
		di "`this_name'"
	}
	
	sort id year
	by id: gen gcm_ivar_prev = gcm_ivar[_n-1]
	by id: gen gcm_ivam_prev = gcm_ivam[_n-1]
	by id: gen gcm_tlf_prev = gcm_tlf[_n-1]
	
	* create obsdate differences for checking correlations
	local variables "obsdate_1p obsdate_2p obsdate_3p obsdate_1m obsdate_2m"
	local shortname "p1 p2 p3 m1 m2"
	local count : word count `variables'
	
	forvalues i=1/`count' {
		forvalues j=`i'/`count' {
			local this_var1 : word `i' of `variables'
			local this_name1 : word `i' of `shortname'
			local this_var2 : word `j' of `variables'
			local this_name2 : word `j' of `shortname'
			if "`this_var1'" != "`this_var2'" {
				gen diff_`this_name1'`this_name2' = `this_var2'-`this_var1'
			}
		}
	}
	
	
	
	* identify teachers that switched subjects
	
	* create post-test indicator
	gen posttest_m2 = 0
	replace posttest_m2 = 1 if year == 2010 & obsdate_2m > date("20100430","YMD")
	replace posttest_m2 = 1 if year == 2011 & obsdate_2m > date("20110414","YMD")
	replace posttest_m2 = 1 if year == 2012 & obsdate_2m > date("20120427","YMD")
	
	gen posttest_p3 = 0
	replace posttest_p3 = 1 if year == 2010 & obsdate_3p > date("20100430","YMD")
	replace posttest_p3 = 1 if year == 2011 & obsdate_3p > date("20110414","YMD")
	replace posttest_p3 = 1 if year == 2012 & obsdate_3p > date("20120427","YMD")
	
	* create average tlf across all years
	sort id year
	bysort id (year): egen mean_tlf = mean(tlf)
	
	* center haz_p2 within year
	sort year
	bysort year: egen hazp2_year_mean = mean(haz_p2)
	gen haz_p2_cent = haz_p2 - hazp2_year_mean
	
	* Combinations of previous evals and mean scores
	* P1
	* M1 can occur before P1
	gen ip1_m1 = obsdate_1m < obsdate_1p
	
	* P2
	* P1 always occurs before, so this is reference group
	gen ip2_p1 = obsdate_1m >= obsdate_2p & !(obsdate_1m ==. | obsdate_2p ==. )
	gen ip2_p1m1 = obsdate_1m < obsdate_2p & obsdate_2p < obsdate_2m & !(obsdate_1m ==. | obsdate_2p ==. )
	gen ip2_p1m1m2 = obsdate_2m <= obsdate_2p & !(obsdate_2m ==. | obsdate_2p ==. )
	
	* P3
	* P2 and P1 and M1 always occur before
	gen ip3_p1p2m1m2 = obsdate_2m < obsdate_3p
	gen ip3_p1p2m1 = !ip3_p1p2m1m2
	
	* M1
	* P1 may occur before
	gen im1_first = obsdate_1m < obsdate_1p
	gen im1_p1 = obsdate_1m > obsdate_1p & obsdate_1m < obsdate_2p
	gen im1_p1p2 = obsdate_1m > obsdate_2p
	
	gen m1_prebreak = ntm1_prebreak > 0
	
	* M2
	* P1 and M1 alway soccur before
	gen im2_p1m1 = obsdate_2m <= obsdate_2p & !(obsdate_2m ==. | obsdate_2p ==. )
	replace im2_p1m1 = . if (obsdate_2m ==. | obsdate_2p ==. | obsdate_2p == .c)
	gen im2_p1p2m1 = obsdate_2m > obsdate_2p & obsdate_2m < obsdate_3p & !(obsdate_2m ==. | obsdate_2p ==. | obsdate_3p==.)
	replace im2_p1p2m1 = . if (obsdate_2m ==. | obsdate_2p ==. | obsdate_3p==.)
	gen im2_p1p2p3m1 = obsdate_2m >= obsdate_3p & !(obsdate_2m ==. | obsdate_3p ==. )
	replace im2_p1p2p3m1 = . if (obsdate_2m ==. | obsdate_3p ==. )
	
	* Means *
	* P1 - can have m1 before
	gen p1_m1 = pr_1m * ip1_m1
		
	* M1 - can have p1 or p1+p2 or none
	gen m1_p1 = pr_1p * im1_p1
	
	egen m1_p1p2 = rowmean(pr_1p pr_2p)
	replace m1_p1p2 = m1_p1p2 * im1_p1p2
	
	gen prev_scores_m1 = m1_p1
	replace prev_scores_m1 = m1_p1p2 if im1_p1p2
	replace prev_scores_m1 = . if !im1_p1 & !im1_p1p2
	
	* P2
	gen p2_p1 = pr_1p * ip2_p1
	
	egen p2_p1m1 = rowmean(pr_1p pr_1m)
	*replace p2_p1m1 = p2_p1m1 * ip2_p1m1
	
	egen p2_p1m1m2 = rowmean(pr_1p pr_1m pr_2m)
	*replace p2_p1m1m2 = p2_p1m1m2 * ip2_p1m1m2
	
	* M2
	egen m2_p1m1 = rowmean(pr_1p pr_1m)
	*replace m2_p1m1 = m2_p1m1 * im2_p1m1
	
	egen m2_p1p2m1 = rowmean(pr_1p pr_2p pr_1m)
	*replace m2_p1p2m1 = m2_p1p2m1 * im2_p1p2m1
	
	egen m2_p1p2p3m1 = rowmean(pr_1p pr_2p pr_3p pr_1m)
	*replace m2_p1p2p3m1 = m2_p1p2p3m1 * im2_p1p2p3m1
	
	gen prev_scores_m2 = m2_p1m1
	replace prev_scores_m2 = m2_p1p2m1 if im2_p1p2m1
	replace prev_scores_m2 = m2_p1p2p3m1 if im2_p1p2p3m1
	
	
	* P3
	egen p3_p1p2m1 = rowmean(pr_1p pr_1m pr_2p)
	*replace p3_p1p2m1 = p3_p1p2m1 * ip3_p1p2m1
	
	egen p3_p1p2m1m2 = rowmean(pr_1p pr_1m pr_2p pr_2m)
	*replace p3_p1p2m1m2 = p3_p1p2m1m2 * ip3_p1p2m1m2
	
	gen prev_scores_p3 = p3_p1p2m1
	replace prev_scores_p3 = p3_p1p2m1m2 if ip3_p1p2m1m2
	
	
	* cap hazard totp
	gen haz_p1_totp_c = haz_p1_totp
	replace haz_p1_totp_c = 1 if haz_p1_totp > 1 & haz_p1_totp != .
	gen haz_p2_totp_c = haz_p2_totp
	replace haz_p2_totp_c = 1 if haz_p2_totp > 1 & haz_p2_totp != .
	gen haz_p3_totp_c = haz_p3_totp
	replace haz_p3_totp_c = 1 if haz_p3_totp > 1 & haz_p3_totp != .
	gen haz_m1_totp_c = haz_m1_totp
	replace haz_m1_totp_c = 1 if haz_m1_totp > 1 & haz_m1_totp != .
	gen haz_m2_totp_c = haz_m2_totp
	replace haz_m2_totp_c = 1 if haz_m2_totp > 1 & haz_m2_totp != .
	
	* create group variable by exp and lastyear
	gen qual_group = .
	replace qual_group = 1 if exp0
	replace qual_group = 2 if tlf_me_lastyear & !(exp0==.|tlf_me_lastyear==.|tlf_e_lastyear==.|tlf_me_lastyear==.)
	replace qual_group = 3 if tlf_e_lastyear & !(exp0==.|tlf_me_lastyear==.|tlf_e_lastyear==.|tlf_me_lastyear==.)
	replace qual_group = 4 if tlf_he_lastyear & !(exp0==.|tlf_me_lastyear==.|tlf_e_lastyear==.|tlf_me_lastyear==.)

	
	* create constant for absorbing in reghdfe without fe
	gen byte constant = 1
	
	* deal with wayward no-threat stuff
	gen timing_p1 = max_ntp1 - nothreat_p1
	replace timing_p1 = . if timing_p1 < 0
	replace nothreat_p1 = . if timing_p1 < 0
	
	gen timing_p2 = max_ntp2 - nothreat_p2
	replace timing_p2 = . if timing_p2 < 0
	replace nothreat_p2 = . if timing_p2 < 0

	gen timing_p3 = max_ntp3_tot - nothreat_p3_all
	replace timing_p3 = . if timing_p3 <0
	replace nothreat_p3_all = . if timing_p3 < 0
	
	gen timing_m1 = max_ntm1 - nothreat_m1
	replace timing_m1 = . if timing_m1 < 0
	replace nothreat_m1 = . if timing_m1 < 0
	
	gen timing_m2 = max_ntm2_tot - nothreat_m2_all
	replace timing_m2 = . if timing_m2 < 0
	replace nothreat_m2_all = . if timing_m2 < 0
	
	gen cap_needed = (haz_p3_totp>1 | haz_p2_totp>1 | haz_m2_totp >1) & (haz_p3_totp!=. & haz_p2_totp!=. & haz_m2_totp!=.)
	
	* fix age
	gen tage_adj = tage
	replace tage_adj = tage-8 if year == 2010
	replace tage_adj = tage-7 if year == 2011
	replace tage_adj = tage-6 if year == 2012

	gen tage_adj_sq = tage_adj * tage_adj
	egen tage_adj_cat = cut(tage_adj), at(0,30,40,50,60,100)
	
	gen nothreat_cycle1_true = nothreat_cycle1
	replace nothreat_cycle1_true = max_ntp1 if year==2010 & nothreat_cycle1!=.
	
	gen pos_ntc3 = nothreat_cycle3 > 0
	gen pos_ntc2 = nothreat_cycle2 > 0
	gen pos_ntc1 = nothreat_cycle1 > 0
	
	* lag no-threat stuff
	sort id year
	foreach var of varlist nothreat_cycle1 nothreat_cycle1_true nothreat_cycle2 nothreat_cycle3 nothreat_p1 nothreat_m1 nothreat_p2 nothreat_m2 nothreat_p3 tlf {
		by id (year): gen `var'_L = `var'[_n-1]
	}
	
	
